<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图片验证码演示</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }

        .container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        .verify-code-section {
            margin: 20px 0;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }

        .image-container {
            margin: 10px 0;
            text-align: center;
        }

        .verify-code-image {
            border: 1px solid #ccc;
            border-radius: 5px;
            cursor: pointer;
        }

        input[type="text"] {
            padding: 8px;
            margin: 5px;
            border: 1px solid #ddd;
            border-radius: 4px;
            width: 200px;
        }

        button {
            padding: 8px 16px;
            margin: 5px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056b3;
        }

        .result {
            margin-top: 10px;
            padding: 10px;
            border-radius: 4px;
        }

        .success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .info {
            background-color: #d1ecf1;
            color: #0c5460;
            border: 1px solid #bee5eb;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>图片验证码演示</h1>

        <div class="verify-code-section">
            <h3>文字验证码</h3>
            <button onclick="getTextVerifyCode()">获取文字验证码</button>
            <div id="textResult"></div>
        </div>

        <div class="verify-code-section">
            <h3>图片验证码</h3>
            <div>
                <label>宽度: <input type="number" id="width" value="120" min="80" max="300"></label>
                <label>高度: <input type="number" id="height" value="40" min="30" max="100"></label>
                <label>长度: <input type="number" id="length" value="4" min="3" max="8"></label>
            </div>
            <button onclick="getImageVerifyCode()">获取图片验证码</button>
            <div id="imageResult"></div>
        </div>

        <div class="verify-code-section">
            <h3>验证码验证</h3>
            <input type="text" id="userInput" placeholder="请输入验证码">
            <input type="text" id="guidInput" placeholder="验证码GUID">
            <button onclick="verifyCode()">验证</button>
            <div id="verifyResult"></div>
        </div>
    </div>

    <script>
        let currentGuid = '';
        let currentCode = '';

        async function getTextVerifyCode() {
            try {
                const response = await fetch('/api/VerifyCode/GetVerifyCode');
                const result = await response.json();

                if (result.code === 200) {
                    currentGuid = result.data.guid;
                    currentCode = result.data.code;
                    document.getElementById('textResult').innerHTML = `
                        <div class="result success">
                            <strong>验证码:</strong> ${result.data.code}<br>
                            <strong>GUID:</strong> ${result.data.guid}
                        </div>
                    `;
                } else {
                    document.getElementById('textResult').innerHTML = `
                        <div class="result error">
                            获取失败: ${result.message}
                        </div>
                    `;
                }
            } catch (error) {
                document.getElementById('textResult').innerHTML = `
                    <div class="result error">
                        请求失败: ${error.message}
                    </div>
                `;
            }
        }

        async function getImageVerifyCode() {
            try {
                const width = document.getElementById('width').value;
                const height = document.getElementById('height').value;
                const length = document.getElementById('length').value;

                const url = `/api/VerifyCode/GetImageVerifyCode?width=${width}&height=${height}&length=${length}`;
                const response = await fetch(url);
                const result = await response.json();

                if (result.code === 200) {
                    currentGuid = result.data.guid;
                    currentCode = result.data.code;
                    document.getElementById('imageResult').innerHTML = `
                        <div class="result success">
                            <strong>GUID:</strong> ${result.data.guid}<br>
                            <strong>验证码:</strong> ${result.data.code}<br>
                            <div class="image-container">
                                <img src="data:image/png;base64,${result.data.imageBase64}" 
                                     alt="验证码" 
                                     class="verify-code-image"
                                     onclick="refreshImage()">
                                <br><small>点击图片刷新</small>
                            </div>
                        </div>
                    `;
                } else {
                    document.getElementById('imageResult').innerHTML = `
                        <div class="result error">
                            获取失败: ${result.message}
                        </div>
                    `;
                }
            } catch (error) {
                document.getElementById('imageResult').innerHTML = `
                    <div class="result error">
                        请求失败: ${error.message}
                    </div>
                `;
            }
        }

        function refreshImage() {
            getImageVerifyCode();
        }

        function verifyCode() {
            const userInput = document.getElementById('userInput').value.toUpperCase();
            const guidInput = document.getElementById('guidInput').value;

            if (!userInput || !guidInput) {
                document.getElementById('verifyResult').innerHTML = `
                    <div class="result error">
                        请输入验证码和GUID
                    </div>
                `;
                return;
            }

            // 这里应该调用后端验证接口，这里只是前端演示
            if (userInput === currentCode && guidInput === currentGuid) {
                document.getElementById('verifyResult').innerHTML = `
                    <div class="result success">
                        验证成功！
                    </div>
                `;
            } else {
                document.getElementById('verifyResult').innerHTML = `
                    <div class="result error">
                        验证失败！请检查验证码和GUID是否正确
                    </div>
                `;
            }
        }

        // 页面加载时获取一个图片验证码
        window.onload = function () {
            getImageVerifyCode();
        };
    </script>
</body>

</html>